package com.snowleopard.findpeople.service.position;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.snowleopard.common.base.BaseDao;
import com.snowleopard.common.base.BaseServiceImpl;
import com.snowleopard.common.param.ParamObject;
import com.snowleopard.common.param.impl.ParamObjectImpl;
import com.snowleopard.findpeople.dao.position.AreaDao;
import com.snowleopard.findpeople.entity.position.Area;

@Service
@Transactional
public class AreaManager extends BaseServiceImpl<Area, String> {

    private final static Logger LOG = LoggerFactory.getLogger(AreaManager.class);

    @Value("#{fpProperties['tree.layer.size']}")
    private Integer layerSize;// 树的层数

    @Autowired
    private AreaDao areaDao;

    @Override
    protected BaseDao<Area, String> getDao() {
        return areaDao;
    }

    public String getTree(String clickedAreaId) {
        StringBuffer treeSB = new StringBuffer();

        Area currentArea = super.get(clickedAreaId);
        Area parent = currentArea.getParent();
        if (parent == null) {
            parent = currentArea;
        }

        treeSB.append("<ul>");

        recursionLayers(treeSB, parent, 1);
        treeSB.append("</ul>");

        LOG.debug("Tree: {}", treeSB.toString());
        return treeSB.toString();
    }

    private void recursionLayers(StringBuffer treeSB, Area area, int layerIndex) {

        String onclick = "onclick=\"displayArea('" + area.getId() + "','" + area.getName() + "')\"";
        // 添加顶点点击事件
        if (layerIndex == 1 && area.getParent() != null) {
            onclick = "onclick=\"clickArea('" + area.getId() + "','" + area.getName() + "')\"";
        }

        if (layerIndex == layerSize.intValue() && area.getChildren().size() > 0) {
            onclick = "onclick=\"clickArea('" + area.getId() + "','" + area.getName() + "')\"";
        }

        if (layerIndex < layerSize && area.getChildren().size() > 0) {

            treeSB.append("<li id=\"" + area.getId() + "\"><a " + onclick + ">" + area.getName()
                    + "</a>");

            if (area.getChildren().size() > 0) {
                treeSB.append("<ul>");

                for (Area child : area.getChildren()) {
                    recursionLayers(treeSB, child, layerIndex + 1);
                }

                treeSB.append("</ul>");
            }

            treeSB.append("</li>");
        } else {

            treeSB.append("<li id=\"" + area.getId() + "\"><a " + onclick + ">" + area.getName()
                    + "</a>");
            treeSB.append("</li>");

        }
    }

    public List<ParamObject> findChildren(String clickedAreaId) {
        List<ParamObject> trades = new ArrayList<ParamObject>();
        List<Area> children = get(clickedAreaId).getChildren();

        for (Area child : children) {
            trades.add(new ParamObjectImpl(child.getId(), child.getName()));
        }
        return trades;
    }
}
